package com.xyz.frame.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.xyz.frame.api.cache.NormalCache;
import com.xyz.frame.common.response.ResponseEnum;
import com.xyz.frame.common.response.ServerResponseEntity;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.shzhyun.TtsService;
import org.shzhyun.opencv.OpenCvTTsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

@RestController
@RequestMapping(path = "/tts")
@Slf4j
public class TtsController {

    @Resource(name = "ttsComponent")
    private TtsService ttsService;

    @RequestMapping(value = "/transe",method = RequestMethod.POST)
    public ServerResponseEntity<String> transe(@RequestBody JSONObject param) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info("===>transe=>request.header:{}",request.getHeader("appTypeId"));
        log.info("transe request:{}",JSONObject.toJSONString(param));
        Integer deviceId = NormalCache.getDeviceIdNum(request.getHeader("deviceId"));
        if(Objects.nonNull(deviceId)){
            return ServerResponseEntity.success(ResponseEnum.SHOW_SUCCESS.value(),"每台设备每天免费一次");
        }

        String videoType = param.getString("videoType");
        String name = "";
        try {
            name = ttsService.doTranse(StringUtils.left(param.getString("text"),200));
            log.info("===>transe=>name:{}",name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        NormalCache.setDeviceIdNumCache(request.getHeader("deviceId"));
        return ServerResponseEntity.success(name);
    }

}
